{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Causal Games.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true,
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/mrklees/pgmpy/blob/feature%2Fcausalmodel/examples/Causal_Games.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "qR-vG2CF4jaz",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Causal Games\n",
        "\n",
        "Causal Inference is a new feature for pgmpy, so I wanted to develop a few examples which show off the features that we're developing! \n",
        "\n",
        "This particular notebook walks through the 5 games that used as examples for building intuition about backdoor paths in *The Book of Why* by Judea Peal. I have consistently been using them to test different implementations of backdoor adjustment from different libraries and include them as unit tests in pgmpy, so I wanted to walk through them and a few other related games as a potential resource to both understand the implementation of CausalInference in pgmpy, as well as develope some useful intuitions about backdoor paths. \n",
        "\n",
        "## Objective of the Games\n",
        "\n",
        "For each game we get a causal graph, and our goal is to identify the set of deconfounders (often denoted $Z$) which will close all backdoor paths from nodes $X$ to $Y$. For the time being, I'll assume that you're familiar with the concept of backdoor paths, though I may expand this portion to explain it. "
      ]
    },
    {
      "metadata": {
        "id": "p1uBjhCQgaaG",
        "colab_type": "code",
        "cellView": "both",
        "outputId": "25270025-2cd2-4ce6-a8e6-7d16e2855492",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 258
        }
      },
      "cell_type": "code",
      "source": [
        "#@title Clone the Development Repo & Install Requirements\n",
        "#@markdown Because the Causal Inference class is currently in dev, we will actually need to pull the code from GitHub.  This cell will give us a pretty good development environment for interactively developing and testing the CausalModel class and its methods.\n",
        "#@markdown You only need to run this the first time you've started the kernel.\n",
        "%%sh\n",
        "git clone https://github.com/mrklees/pgmpy.git\n",
        "mv /content/pgmpy /content/pgmpydev\n",
        "cd pgmpydev/\n",
        "git checkout feature/causalmodel\n",
        "git pull\n",
        "#@markdown In testing the CausalModel and Bayesian Network portion of pgmpy we've actually been able to use up to date version of Networkx and other packages, but we may be forced to downgrade to networkx 1.11 if errors arise.\n",
        "#pip install -U -r requirements-dev.txt"
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.6/dist-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.\n",
            "  from pandas.core import datetools\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Your branch is up to date with 'origin/feature/causalmodel'.\n",
            "Updating c459420..95af23b\n",
            "Fast-forward\n",
            " pgmpy/inference/causal_inference.py                 | 14 +++++++-------\n",
            " pgmpy/tests/test_inference/test_causal_inference.py |  2 +-\n",
            " 2 files changed, 8 insertions(+), 8 deletions(-)\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "fatal: destination path 'pgmpy' already exists and is not an empty directory.\n",
            "mv: cannot move '/content/pgmpy' to '/content/pgmpydev/pgmpy': Directory not empty\n",
            "Already on 'feature/causalmodel'\n",
            "From https://github.com/mrklees/pgmpy\n",
            "   c459420..95af23b  feature/causalmodel -> origin/feature/causalmodel\n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "metadata": {
        "id": "EkFVGD3Tv7ma",
        "colab_type": "code",
        "cellView": "both",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "a3fd160d-dcdf-4495-e8ea-e5b3aedadaf5"
      },
      "cell_type": "code",
      "source": [
        "#@title Imports\n",
        "%load_ext autoreload\n",
        "%autoreload 2\n",
        "import sys\n",
        "sys.path.append('/content/pgmpydev')\n",
        "\n",
        "!pip3 install -q daft\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline\n",
        "import daft\n",
        "from daft import PGM\n",
        "\n",
        "# We can now import the development version of pgmpy\n",
        "from pgmpy.models.BayesianModel import BayesianModel\n",
        "from pgmpy.inference.causal_inference import CausalInference\n",
        "\n",
        "def convert_pgm_to_pgmpy(pgm):\n",
        "    \"\"\"Takes a Daft PGM object and converts it to a pgmpy BayesianModel\"\"\"\n",
        "    edges = [(edge.node1.name, edge.node2.name) for edge in pgm._edges]\n",
        "    model = BayesianModel(edges)\n",
        "    return model"
      ],
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "The autoreload extension is already loaded. To reload it, use:\n",
            "  %reload_ext autoreload\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "P90trQAQ7Clc",
        "colab_type": "code",
        "cellView": "form",
        "outputId": "2e62d5b8-de0e-4e28-b14d-5c03bcd1ab61",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 201
        }
      },
      "cell_type": "code",
      "source": [
        "#@title # Game 1\n",
        "#@markdown While this is a \"trivial\" example, many statisticians would consider including either or both A and B in their models \"just for good measure\".  Notice though how controlling for A would close off the path of causal information from X to Y, actually *impeding* your effort to measure that effect.\n",
        "pgm = PGM(shape=[4, 3])\n",
        "\n",
        "pgm.add_node(daft.Node('X', r\"X\", 1, 2))\n",
        "pgm.add_node(daft.Node('Y', r\"Y\", 3, 2))\n",
        "pgm.add_node(daft.Node('A', r\"A\", 2, 2))\n",
        "pgm.add_node(daft.Node('B', r\"B\", 2, 1))\n",
        "\n",
        "\n",
        "pgm.add_edge('X', 'A')\n",
        "pgm.add_edge('A', 'Y')\n",
        "pgm.add_edge('A', 'B')\n",
        "\n",
        "pgm.render()\n",
        "plt.show()"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPEAAAC4CAY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/Pbx7hMJIYlfg34hrv/vBdt\nlvooxC1S7oX3BPYmArsDMAJYQfS9nlu+HtbTHtpDIRbJnM6JRTKnEItkTiEWyZxCLJI5hVgkcwqx\nSOYUYpHMKcQimVOIRTKnEItkTiEWyZxCLJI5hVgkcwqxSOYUYpHMKcQimVOIRTKnEItkTiEWyZxC\nLJI5hVgkcwqxSOYUYpHMKcQimVOIRTKnEItkTiEWyZxCLJI5hVgkcwqxSOYUYpHMKcQimVOIRTKn\nEItkTiEWyZxCLJI5hVgkcwqxSOYUYpHMKcQimVOIRTKnEItkTiEWyZxCLJI5hVgkcwqxSOYUYpHM\nKcQimft/k4wcZaGmP/YAAAAASUVORK5CYII=\n",
            "text/plain": [
              "<Figure size 226.772x170.079 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "metadata": {
        "id": "yQyYJEC83ODX",
        "colab_type": "code",
        "outputId": "07597081-bbec-4209-db8f-b731de34ae3a",
        "cellView": "both",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "cell_type": "code",
      "source": [
        "#@markdown Notice how there are no nodes with arrows pointing into X.  Said another way, X has no parents.  Therefore, there can't be any backdoor paths confounding X and Y.  pgmpy will confirm this in the following way:\n",
        "game1 = convert_pgm_to_pgmpy(pgm)\n",
        "inference1 = CausalInference(game1)\n",
        "print(f\"Are there are active backdoor paths? {inference1._has_active_backdoors('X', 'Y')}\")\n",
        "adj_sets = inference1.get_all_backdoor_adjustment_sets(\"X\", \"Y\")\n",
        "print(f\"If so, what's the possible backdoor adjustment sets? {adj_sets}\")"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Are there are active backdoor paths? False\n",
            "If so, what's the possible backdoor adjustment sets? frozenset()\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "b5RJ0UsH_kQ4",
        "colab_type": "code",
        "cellView": "form",
        "outputId": "3e4f9430-fca7-40d4-f8b3-ffd241c6a8f8",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 258
        }
      },
      "cell_type": "code",
      "source": [
        "#@title # Game 2\n",
        "#@markdown This graph looks harder, but actualy is also trivial to solve.  The key is noticing the one backdoor path, which goes from X <- A -> B <- D -> E -> Y, has a collider at B (or a 'V structure'), and therefore the backdoor path is closed. \n",
        "pgm = PGM(shape=[4, 4])\n",
        "\n",
        "pgm.add_node(daft.Node('X', r\"X\", 1, 1))\n",
        "pgm.add_node(daft.Node('Y', r\"Y\", 3, 1))\n",
        "pgm.add_node(daft.Node('A', r\"A\", 1, 3))\n",
        "pgm.add_node(daft.Node('B', r\"B\", 2, 3))\n",
        "pgm.add_node(daft.Node('C', r\"C\", 3, 3))\n",
        "pgm.add_node(daft.Node('D', r\"D\", 2, 2))\n",
        "pgm.add_node(daft.Node('E', r\"E\", 2, 1))\n",
        "\n",
        "\n",
        "pgm.add_edge('X', 'E')\n",
        "pgm.add_edge('A', 'X')\n",
        "pgm.add_edge('A', 'B')\n",
        "pgm.add_edge('B', 'C')\n",
        "pgm.add_edge('D', 'B')\n",
        "pgm.add_edge('D', 'E')\n",
        "pgm.add_edge('E', 'Y')\n",
        "\n",
        "pgm.render()\n",
        "plt.show()"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPEAAADxCAYAAAAay1EJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAGkFJREFUeJztnXn4XtO1xz8rI5KYQwlKDCeGCEnE\nUJFQw9XWrfGa3dCiJLeToUXog7qqqKaK0tbU1hRVrVIzEU0NiZlmI6KIoeYaYl73j7V/lRtJfud9\n33Pe89sn6/M875O8yTlrrf2e8z17n73X3ltUFcdx0qVb1Q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X0u\n8CtVfaGKmBtFRNbAduQYA0zH8gI6tnH5ENvhcl2sjKOwh/DZwBWq2ujkiKSovYjnRERWwdZx6thQ\nrR82m+U14oZqwJ1awiqL7SC+Ow7FpvwNA9bA1qz6AKutp8XPFE10v+KYlbc5Vr6ODdV6YrO+pmPl\nu0tVC8377sosVCJ2nDpSy44tx1mYcBE7TuK4iB0ncVzEjpM4LmLHSRwXseMkjovYcRLHRew4ieMi\ndpzEcRE7TuK4iB0ncVzEjpM4LmLHSRwXseMkjovYcRLHRew4ieMidpzEcRE7TuK4iB0ncVzEjpM4\nLmLHSRwXseMkjovYcRLHRew4ieMidpzEcRE7TuK4iB0ncVzEjpM4LmLHSRwXseMkjovYcRLHRew4\nieMidpzEcRE7TuK4iB0ncVzEjpM4LmLHSRwXseMkjovYcRLHRew4ieMidpzEcRE7TuK4iB0ncVzE\njpM4LmLHSRwXseMkjovYcRLHRew4ifN/jHxgrkKc1LAAAAAASUVORK5CYII=\n",
            "text/plain": [
              "<Figure size 226.772x226.772 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "metadata": {
        "id": "2d6Ezs6PDDON",
        "colab_type": "code",
        "outputId": "c597c0df-3271-4e8e-9a68-3200c7f105a5",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "cell_type": "code",
      "source": [
        "graph = convert_pgm_to_pgmpy(pgm)\n",
        "inference = CausalInference(graph)\n",
        "print(f\"Are there are active backdoor paths? {inference._has_active_backdoors('X', 'Y')}\")\n",
        "adj_sets = inference.get_all_backdoor_adjustment_sets(\"X\", \"Y\")\n",
        "print(f\"If so, what's the possible backdoor adjustment sets? {adj_sets}\")"
      ],
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Are there are active backdoor paths? False\n",
            "If so, what's the possible backdoor adjustment sets? frozenset()\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Pg6T2WA3DZ8n",
        "colab_type": "code",
        "cellView": "form",
        "outputId": "5d79efd3-b8f7-46a8-dcf0-da2b7997be3c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 258
        }
      },
      "cell_type": "code",
      "source": [
        "#@title # Game 3\n",
        "#@markdown This game actually requires some action.  Notice the backdoor path X <- B -> Y.  This is a confounding pattern, is one of the clearest signs that we'll need to control for something, in this case B. \n",
        "pgm = PGM(shape=[4, 4])\n",
        "\n",
        "pgm.add_node(daft.Node('X', r\"X\", 1, 1))\n",
        "pgm.add_node(daft.Node('Y', r\"Y\", 3, 1))\n",
        "pgm.add_node(daft.Node('A', r\"A\", 2, 1.75))\n",
        "pgm.add_node(daft.Node('B', r\"B\", 2, 3))\n",
        "\n",
        "\n",
        "pgm.add_edge('X', 'Y')\n",
        "pgm.add_edge('X', 'A')\n",
        "pgm.add_edge('B', 'A')\n",
        "pgm.add_edge('B', 'X')\n",
        "pgm.add_edge('B', 'Y')\n",
        "\n",
        "pgm.render()\n",
        "plt.show()"
      ],
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPEAAADxCAYAAAAay1EJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAFKxJREFUeJzt3X/0Z1O9x/Hne3wlw6TkRzW4fuX4\nkTs0QshQIV0tolYhwi3dhKRcinJTuusWq+sS+XGVcsMoJS3cuAzGhMbvX3NakSu/iluEBtN43z/2\n/pjPqvnO95zP5+zz+ezPvB5rfdfM1/fsvc/4ft/f1/nsz9n7mLsjIvmaNOgTEJH+qIhFMqciFsmc\nilgkcypikcypiEUypyIWyZyKWCRzKmKRzKmIRTKnIhbJnIpYJHMqYpHMqYhFMqciFsmcilgkcypi\nkcypiEUypyIWyZyKW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TvN7Otmtu5E7eJT\nKXc3s6uAEwg7nx43igUMIzaxtThxT6X3Ap8krJ65nnCj/Z2EVTOTgFUJz/bZIh5zCeESrpcb8iUB\nM1sf+CfgAMJl8S0seozLAsJztTYhrBGeAfyWMI8x093rLo7IysgXcbf4eI9tWfRAtSmE1SyvPFAN\nmO0JdlmUZsSrqe0I38POA9WWJTy9ch7he3iTu98/sJNs2VJVxCKjaGRfE4ssLVTEIplTEYtkTkUs\nkjkVsUjmVMQimVMRi2RORSySORWxSOZUxCKZUxGLZE5FLJI5FbFI5lTEIplTEYtkTkUskjkVsUjm\nVMQimVMRi2RORSySORWxSOZUxCKZUxGLZE5FLJI5FbFI5lTEIplTEYtkTkUskjkVsUjmVMQimVMR\ni2RORSySORWxSOZUxCKZUxGLZE5FLJI5FbFI5lTEIplTEYtkTkUskjkVsUjmVMQimVMRi2RORSyS\nORWxSOZUxCKZUxGLZE5FLJK5/weF62ecIGohUQAAAABJRU5ErkJggg==\n",
            "text/plain": [
              "<Figure size 226.772x226.772 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "metadata": {
        "id": "l0GI2mM3WQeI",
        "colab_type": "code",
        "outputId": "7ca879e8-e678-42d9-9d03-404ffb67c51c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "cell_type": "code",
      "source": [
        "graph = convert_pgm_to_pgmpy(pgm)\n",
        "inference = CausalInference(graph)\n",
        "print(f\"Are there are active backdoor paths? {inference._has_active_backdoors('X', 'Y')}\")\n",
        "adj_sets = inference.get_all_backdoor_adjustment_sets(\"X\", \"Y\")\n",
        "print(f\"If so, what's the possible backdoor adjustment sets? {adj_sets}\")"
      ],
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Are there are active backdoor paths? True\n",
            "If so, what's the possible backdoor adjustment sets? frozenset({frozenset({'B'})})\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "XP2ORZw8EtyZ",
        "colab_type": "code",
        "cellView": "form",
        "outputId": "9f267191-b408-483b-87d9-754c949a7d72",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 258
        }
      },
      "cell_type": "code",
      "source": [
        "#@title # Game 4\n",
        "#@markdown Pearl named this particular configuration \"M Bias\", not only because of it's shape, but also because of the common practice of statisticians to want to control for B in many situations.  However, notice how in this configuration X and Y start out as *not confounded* and how by controlling for B we would actually introduce confounding by opening the path at the collider, B. \n",
        "pgm = PGM(shape=[4, 4])\n",
        "\n",
        "pgm.add_node(daft.Node('X', r\"X\", 1, 1))\n",
        "pgm.add_node(daft.Node('Y', r\"Y\", 3, 1))\n",
        "pgm.add_node(daft.Node('A', r\"A\", 1, 3))\n",
        "pgm.add_node(daft.Node('B', r\"B\", 2, 2))\n",
        "pgm.add_node(daft.Node('C', r\"C\", 3, 3))\n",
        "\n",
        "\n",
        "pgm.add_edge('A', 'X')\n",
        "pgm.add_edge('A', 'B')\n",
        "pgm.add_edge('C', 'B')\n",
        "pgm.add_edge('C', 'Y')\n",
        "\n",
        "pgm.render()\n",
        "plt.show()"
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPEAAADxCAYAAAAay1EJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAFO1JREFUeJzt3Xm4HUWdxvHvLyEhEBZBFmUXwUJA\niAYiQsJqwOCgMDFjCMIAThACbgjiDAEJo44wZBQHgyCyiDygKDKiuATFkAxgSCCsk1ZZRILKkgEF\ngmw//6i+5opJbvc93eec6vN+nuc+z73J6aq6t897urq6u8rcHRFJ15BON0BEWqMQiyROIRZJnEIs\nkjiFWCRxCrFI4hRikcQpxCKJU4hFEqcQiyROI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TWZ2YKtHZDObAHwdOMzdtfBaG7j7krzbOzuf8XJWKwOJZrYR\n8RnlxcDJFTWzazTqSNzH3c8hzjs818xOHMxgl5mtbWbnAxcAh7j7T6pup6xcPtvoXsSj8nX5AgCl\n5adBdxHnrJ6aP4XWKI0MMcQjMrAb8F5ggZkdVeQ6spltaGafAu4DhhPneppXb2tlRdz918CuxJUe\n7jSzs81s64G2M7PVzOy9ZjabOO/0we4+vYkBhoYNbK1IPqfSgcBxwNuJc2QtIC4D8jTxg2xD4to+\nu+SvuYbYhevZlfa6jZltQ1yR40hit3g+MdxLgBeJK1zuQHxGeC/gt8RxjG+5e9mHI5LS+BD3Z2Zb\nEC8v9C2otjbxaZal5AuqAfPqmGVRqpH3psYS9+FoYCPiQuPPEcO9ELjV3f+vY41ss54KsUgTNfac\nWKRXKMQiiVOIRRKnEIskTiEWSZxCLJI4hVgkcQqxSOIUYpHEKcQiiVOIRRKnEIskTiEWSZxCLJI4\nhVgkcQqxSOIUYpHEKcQiiVOIRRKnEIskTiEWSZxCLJI4hVgkcQqxSOIUYpHEKcQiiVOIRRKnEIsk\nTiEWSZxCLJI4hVgkcQqxSOIUYpHEKcQiiVOIRRKnEIskTiEWSZxCLJI4hVgkcQqxSOIUYpHEKcQi\niVOIRRKnEIskTiEWSZxCLJI4hVgkcQqxSOL+AgEKR0Xy3CYuAAAAAElFTkSuQmCC\n",
            "text/plain": [
              "<Figure size 226.772x226.772 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "metadata": {
        "id": "CBaGzLKSFmnQ",
        "colab_type": "code",
        "outputId": "95c1c6a5-dbe3-4768-8f37-94d9547e9db6",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "cell_type": "code",
      "source": [
        "graph = convert_pgm_to_pgmpy(pgm)\n",
        "inference = CausalInference(graph)\n",
        "print(f\"Are there are active backdoor paths? {inference._has_active_backdoors('X', 'Y')}\")\n",
        "adj_sets = inference.get_all_backdoor_adjustment_sets(\"X\", \"Y\")\n",
        "print(f\"If so, what's the possible backdoor adjustment sets? {adj_sets}\")"
      ],
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Are there are active backdoor paths? False\n",
            "If so, what's the possible backdoor adjustment sets? frozenset()\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "ZAbSVPvZFxZH",
        "colab_type": "code",
        "cellView": "form",
        "outputId": "88a7fff6-0744-4dda-b5e9-8d93dcea84d7",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 258
        }
      },
      "cell_type": "code",
      "source": [
        "#@title # Game 5\n",
        "#@markdown This is the last game in The Book of Why is the most complex.  In this case we have two backdoor paths, one going through A and the other through B, and it's important to notice that if we only control for B that the path: X <- A -> B <- C -> Y (which starts out as closed because B is a collider) actually is opened.  Therefore we have to either close both A and B or, as astute observers will notice, we can also just close C and completely close both backdoor paths.  pgmpy will nicely confirm these results for us. \n",
        "pgm = PGM(shape=[4, 4])\n",
        "\n",
        "pgm.add_node(daft.Node('X', r\"X\", 1, 1))\n",
        "pgm.add_node(daft.Node('Y', r\"Y\", 3, 1))\n",
        "pgm.add_node(daft.Node('A', r\"A\", 1, 3))\n",
        "pgm.add_node(daft.Node('B', r\"B\", 2, 2))\n",
        "pgm.add_node(daft.Node('C', r\"C\", 3, 3))\n",
        "\n",
        "\n",
        "pgm.add_edge('A', 'X')\n",
        "pgm.add_edge('A', 'B')\n",
        "pgm.add_edge('C', 'B')\n",
        "pgm.add_edge('C', 'Y')\n",
        "pgm.add_edge(\"X\", \"Y\")\n",
        "pgm.add_edge(\"B\", \"X\")\n",
        "\n",
        "pgm.render()\n",
        "plt.show()"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPEAAADxCAYAAAAay1EJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/A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bKMQiiVOIRRKnEIskTiEWSZxCLJI4hVgkcQqxSOIUYpHEKcQiiVOIRRKnEIskTiEWSZxCLJI4\nhVgkcQqxSOIUYpHEKcQiiVOIRRKnEIskTiEWSZxCLJI4hVgkcQqxSOIUYpHEKcQiiVOIRRKnEIsk\nTiEWSZxCLJI4hVgkcQqxSOIUYpHEKcQiiVOIRRKnEIskTiEWSZxCLJI4hVgkcQqxSOIUYpHEKcQi\niVOIRRKnEIskTiEWSZxCLJI4hVgkcQqxSOL+H73NVvPwcCIZAAAAAElFTkSuQmCC\n",
            "text/plain": [
              "<Figure size 226.772x226.772 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "metadata": {
        "id": "IF1jYMq_eNHd",
        "colab_type": "code",
        "outputId": "68bcc15d-c1aa-40c4-fb61-b400e7c31abe",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        }
      },
      "cell_type": "code",
      "source": [
        "graph = convert_pgm_to_pgmpy(pgm)\n",
        "inference = CausalInference(graph)\n",
        "print(f\"Are there are active backdoor paths? {inference._has_active_backdoors('X', 'Y')}\")\n",
        "adj_sets = inference.get_all_backdoor_adjustment_sets(\"X\", \"Y\")\n",
        "print(f\"If so, what's the possible backdoor adjustment sets? {adj_sets}\")"
      ],
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Are there are active backdoor paths? True\n",
            "If so, what's the possible backdoor adjustment sets? frozenset({frozenset({'B', 'A'}), frozenset({'C'})})\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "dSjZqd5fHF06",
        "colab_type": "code",
        "cellView": "form",
        "outputId": "05857131-230d-4dd9-88d0-25d4b800eb68",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 258
        }
      },
      "cell_type": "code",
      "source": [
        "#@title # Game 6\n",
        "#@markdown So these are no longer drawn from The Book of Why, but were either drawn from another source (which I will reference) or a developed to try to induce a specific bug. \n",
        "#@markdown This example is drawn from Causality by Pearl on p. 80. This example is kind of interesting because there are many possible combinations of nodes which will close the two backdoor paths which exist in this graph.  In turns out that D plus any other node in {A, B, C, E} will deconfound X and Y. \n",
        "\n",
        "pgm = PGM(shape=[4, 4])\n",
        "\n",
        "pgm.add_node(daft.Node('X', r\"X\", 1, 1))\n",
        "pgm.add_node(daft.Node('Y', r\"Y\", 3, 1))\n",
        "pgm.add_node(daft.Node('A', r\"A\", 1, 3))\n",
        "pgm.add_node(daft.Node('B', r\"B\", 3, 3))\n",
        "pgm.add_node(daft.Node('C', r\"C\", 1, 2))\n",
        "pgm.add_node(daft.Node('D', r\"D\", 2, 2))\n",
        "pgm.add_node(daft.Node('E', r\"E\", 3, 2))\n",
        "pgm.add_node(daft.Node('F', r\"F\", 2, 1))\n",
        "\n",
        "\n",
        "pgm.add_edge('X', 'F')\n",
        "pgm.add_edge('F', 'Y')\n",
        "pgm.add_edge('C', 'X')\n",
        "pgm.add_edge('A', 'C')\n",
        "pgm.add_edge('A', 'D')\n",
        "pgm.add_edge('D', 'X')\n",
        "pgm.add_edge('D', 'Y')\n",
        "pgm.add_edge('B', 'D')\n",
        "pgm.add_edge('B', 'E')\n",
        "pgm.add_edge('E', 'Y')\n",
        "\n",
        "pgm.render()\n",
        "plt.show()"
      ],
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPEAAADxCAYAAAAay1EJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAHr9JREFUeJztnXmcHVWVx78n+0LYV4EQwlIQlgAJ\niwRIWCNoFIHIJgyokZ1RBgRlkTDiMsqHAYcgiOwIGFkURSAIRCIgJGHHlBoY2VSCGYFAEAhn/ji3\nSdt0uqveq+VVvfP9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8FvaK\n56sZ2kxNmGv4IvA5ETm8t+OTIiJDgduA6XUSMNRMxACqejkmuN+EcCxNISJ7Y0HkDlXVphKvZUFo\n5AcDqwA3iMhyzdgTkb4i8h3gIGAvbYHA8mETxETgWyJyXKf11Q0hIqtjwQD/AJySQRFbitqJGEBV\nv4fFHb5fRE5qZLJLRIaJyMXY89OnVfWurMvZKGEN9SRsxdHjIjK+ETsisinwW2zt+M6aMidVnoRn\n/vFYr3xbSACQmvAY9AQ2XzJFSw4CmAvaAiE38/pg645nYrtjjgQGJzhnNeA0bBfPj4AVyq5HL+Wd\nhIVjnQ5MIMxz9HLOaOAH2CaAY4A+Zdejh7IOwJbELsAicoxMcE4/4FPYK6TfYzHDSq9LXp9aTWx1\nR5i13gdrrNtjMbJmY2lAXsNGI6thuX3GhmNuBqZpRaI+hAB6hwHHYQ14FrY+OcbWKg9gaRqXHbBY\n05cAl6nqX8ooc1pEZEMsI8cRwDxsXcAcbH7gXSzD5WZYHcdjN+FpwE9UNe3miEpRexF3RkSGY3Gc\nOhKqDcN2sywkJFQDZmkOURaLIDw7boNt+RsDbIjFrHoH663nhM8D2kPCslYmrMrbCavfGGB1bMfV\nW5i45wAPqWqm675bmbYSsePUkVpObDlOO+EidpyK4yJ2nIrjInaciuMidpyK4yJ2nIrjInaciuMi\ndpyK4yJ2nIrjInaciuMidpyK4yJ2nIrjInaciuMidpyK4yJ2nIrjInaciuMidpyK4yJ2nIrjInac\niuMidpyK4yJ2nIrjInaciuMidpyK4yJ2nIrjInaciuMidpyK4yJ2nIrjInaciuMidpyK4yJ2nIrj\nInaciuMidpyK4yJ2nIrjInaciuMidpyK4yJ2nIrjInaciuMidpyK4yJ2nIrjInaciuMidpyK4yJ2\nnIrjInaciuMidpyK4yJ2nIrjInaciuMidpyK4yJ2nIrz//fZsGHg3Z2QAAAAAElFTkSuQmCC\n",
            "text/plain": [
              "<Figure size 226.772x226.772 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "metadata": {
        "id": "30OIiRt7raN2",
        "colab_type": "code",
        "outputId": "4c285443-724c-4988-b9a6-2ad2fcf2654e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "cell_type": "code",
      "source": [
        "graph = convert_pgm_to_pgmpy(pgm)\n",
        "inference = CausalInference(graph)\n",
        "print(f\"Are there are active backdoor paths? {inference._has_active_backdoors('X', 'Y')}\")\n",
        "bd_adj_sets = inference.get_all_backdoor_adjustment_sets(\"X\", \"Y\")\n",
        "print(f\"If so, what's the possible backdoor adjustment sets? {bd_adj_sets}\")\n",
        "fd_adj_sets = inference.get_all_frontdoor_adjustment_sets(\"X\", \"Y\")\n",
        "print(f\"Ehat's the possible front adjustment sets? {fd_adj_sets}\")"
      ],
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Are there are active backdoor paths? True\n",
            "If so, what's the possible backdoor adjustment sets? frozenset({frozenset({'B', 'D'}), frozenset({'A', 'D'}), frozenset({'C', 'D'}), frozenset({'E', 'D'})})\n",
            "Ehat's the possible front adjustment sets? frozenset({frozenset({'F'})})\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Z4pkuyOwM9xq",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 201
        },
        "cellView": "form",
        "outputId": "82e581df-b378-423c-95bd-d16b901c5296"
      },
      "cell_type": "code",
      "source": [
        "#@title # Game 7\n",
        "#@markdown This game tests the front door adjustment.  B is taken to be unobserved, and therfore we cannot close the backdoor path X <- B -> Y. \n",
        "pgm = PGM(shape=[4, 3])\n",
        "\n",
        "pgm.add_node(daft.Node('X', r\"X\", 1, 1))\n",
        "pgm.add_node(daft.Node('Y', r\"Y\", 3, 1))\n",
        "pgm.add_node(daft.Node('A', r\"A\", 2, 1))\n",
        "pgm.add_node(daft.Node('B', r\"B\", 2, 2))\n",
        "\n",
        "\n",
        "pgm.add_edge('X', 'A')\n",
        "pgm.add_edge('A', 'Y')\n",
        "pgm.add_edge('B', 'X')\n",
        "pgm.add_edge('B', 'Y')\n",
        "\n",
        "pgm.render()\n",
        "plt.show()"
      ],
      "execution_count": 40,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPEAAAC4CAYAAAAynAqtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAEMpJREFUeJzt3Xm0HGWZx/HvE5IQBAxEFtkCBuQV\nEAIk7LsCGRGOwBBlH2BYZNFBhcEZNgGZGQScYY7CEJQgg7JElIMMIoMsgoiQQNiE4ogsAgpxGQQJ\nEOCZP57qIYMht6tvVXe/nd/nnHuSe2/Xu9yuX1fVW29VmbsjIvka0esGiMjwKMQimVOIRTKnEItk\nTiEWyZxCLJI5hVgkcwqxSOYUYpHMKcQimVOIRTKnEItkTiEWyZxCLJI5hVgkcwqxSOYUYpHMKcQi\nmVOIRTKnEItkTiEWyZxCLJI5hVgkcwqxSOYUYpHMKcQimVOIRTKnEItkTiEWyZxCLJI5hVgkcwqx\nSOYUYpHMKcQimVOIRTKnEItkTiEWyZxCLJI5hVgkcwqxSOYUYpHMKcQimVOIRTKnEItkTiEWyZxC\nLJI5hVgkcyN73QBphpmtCEwAxgCvA88CT7m797RhUjvTezoYzGwksCtwALApsCTwGPAqMBpYo/x3\nJjADuNzdX+lJY6VWCnHmzGwx4BjgOOAp4CLgduCJd251zWwlYAvgIGDL8rVnKMx5U4gzZmYJuBiY\nBxzr7rMrLLsGcCax1T7Y3e9ooo3SPA1sZcrMdgTuAC4HPlIlwADu/qS77wccD8wws8MbaKZ0gQa2\nMmRmHwG+A+zp7rcPpyx3v8bMHgJuMjPcfVotjZSu0e50ZsxsNWAWMNXdb6ux3LWIY+mp2rXO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dc5Uq+D/A+4AozqzJJ5S+Y\n2WJmdhZx+mln74Mby5fnzKcA/2xmR883v7ojZrYCcTPAx4Dja2hiXxm4EAO4+znEfYdvN7PPdzLY\nZWZLm9kFxPHTHu5+Y93t7FQ5+WM3YsbR/Wa2XSflmNk6wE+Jc6bbuPvz9bVyeMpj/u2IrfIPzGzl\nTsopD4MeIMZLDvMe3wSwEd4Ht9xs6ouYd3wbcXXMwcASbSyzPPBF4iqebwJje92PIdq7GzGbawaw\nPeU4xxDLTAT+g7gI4EhgRK/7sZC2jiZm080h7sgxoY1lRgKfIE4hPULcM6znfWnqa6AGthakHLXe\nhVhZNyPukTUTuJ+YqzyCCO7GxBZpM+JeXee7e9Xrd3uivIHeAcDRxAp8BzEbqSDmKo8GPkDM+Nqc\nuNf0hcA33P03vWhzVWa2FvFEjoOAR4l5AbOI8YF5xHO11iP6uB3xIXw+cJW7V51XnZWBD/H8ypld\nW/H2A9WWJq5m+b8HqgF3eAN3WeyG8thxY+KSv0nAWsQ9q14nttazyq87fSEPLOtn5QPYtib613qg\n2ijiqq9Hif7d5Q3fxqifLFIhFhlEAzmwJbIoUYhFMqcQi2ROIRbJnEIskjmFWCRzCrFI5hRikcwp\nxCKZU4hFMqcQi2ROIRbJnEIskjmFWCRzCrFI5hRikcwpxCKZU4hFMqcQi2ROIRbJnEIskjmFWCRz\nCrFI5hRikcwpxCKZU4hFMqcQi2ROIRbJnEIskjmFWCRzCrFI5hRikcwpxCKZU4hFMqcQi2ROIRbJ\nnEIskjmFWCRzCrFI5hRikcwpxCKZU4hFMqcQi2ROIRbJnEIskjmFWCRzCrFI5hRikcz9L/I0184T\nXIh7AAAAAElFTkSuQmCC\n",
            "text/plain": [
              "<Figure size 226.772x170.079 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "metadata": {
        "id": "m8DZd_FQ4uLV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "b73751d4-8aa9-4ec1-ba10-d509fe13c1a1"
      },
      "cell_type": "code",
      "source": [
        "graph = convert_pgm_to_pgmpy(pgm)\n",
        "inference = CausalInference(graph)\n",
        "print(f\"Are there are active backdoor paths? {inference._has_active_backdoors('X', 'Y')}\")\n",
        "bd_adj_sets = inference.get_all_backdoor_adjustment_sets(\"X\", \"Y\")\n",
        "print(f\"If so, what's the possible backdoor adjustment sets? {bd_adj_sets}\")\n",
        "fd_adj_sets = inference.get_all_frontdoor_adjustment_sets(\"X\", \"Y\")\n",
        "print(f\"Ehat's the possible front adjustment sets? {fd_adj_sets}\")"
      ],
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Are there are active backdoor paths? True\n",
            "If so, what's the possible backdoor adjustment sets? frozenset({frozenset({'B'})})\n",
            "Ehat's the possible front adjustment sets? frozenset({frozenset({'A'})})\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "8zeU4DX0Bbxl",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}